package com.plugin.sproutcore.core.resources;

import java.io.IOException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;

import com.plugin.sproutcore.core.resources.properties.PropertiesLoader;
import com.plugin.sproutcore.core.resources.properties.SproutCoreProperties;
import com.plugin.sproutcore.util.ProjectUtil;

/**
 * The SproutCore context.
 */
public class SproutCoreContext 
{
	/**
	 * Project that contains the sprout core nature
	 */
	private final IProject project;
	
	/**
	 * Set the project reference.
	 * @param project Project reference.
	 */
	public SproutCoreContext(IProject project) 
	{
		this.project = project;
	}
	/**
	 * Get the project reference.
	 * @return The project reference.
	 */
	public IProject getProject()
	{
		return this.project;
	}
	
	/**
	 * Get the SproutCore properties.
	 * 
	 * @return The properties.
	 * @throws CoreException
	 */
	public SproutCoreProperties getProperties() 
	throws CoreException
	{
		SproutCoreProperties properties = null;
		if (project != null) 
		{
			try {
				properties = PropertiesLoader.load(project);
			} catch (IOException e) {
				throw new CoreException(ProjectUtil.createErrorStatus(
						"Could not load SproutCore properties for: " + 
						project.getName() + "\nReason: " + e.getMessage()));
			}
		}
		return properties;
	}
}
